﻿String.prototype.trim = function(){
	return this.replace(/(^\s+)|(\s+$)/g, '');
}

function tokenKey(){
	var hex = new Array('0','1','2','3','4','5','6','7','8', '9','a','b','c','d','e','f');
	var t = '';
	for (i = 0; i<32; i++){
		t += hex[Math.floor(Math.random() * 16)];
	}
	return t.toUpperCase();
}

//添加：Jiang 判断对象是否为NULL
function IsNull(o)
{
    return ("undefined" == typeof(o) || "unknown" == typeof(o) || null == o)
}

//添加：Jiang 判断值是否为NULL或空
function IsNullOrEmpty(v)
{
    return ( v == null || v == "undefined" || v == "")
}

//添加：Jiang  控制输入框只能为数字
function isDigit()
{
    return ((event.keyCode >= 48) && (event.keyCode <= 57));
}


/* groble config */
var _sWebServicesNamespace = 'http://www.ce.net.cn';
var LOCID_GRID_SELECT_ALL_KEY = "A";
var LOCID_XML_PARSER_ERROR = "XML \u5206\u6790\u7a0b\u5e8f\u9519\u8bef\u003a\u007b0\u007d";
var LOCID_LOADING_GRID_DATA = "\u6b63\u5728\u52a0\u8f7d\u002e\u002e\u002e";
var LOCID_REFRESH_GRID_CANCELLED = "\u5237\u65b0\u5df2\u53d6\u6d88\u3002";
var LOCID_LOADING_GRID_FAILED = "\u7f51\u683c\u5237\u65b0\u5931\u8d25\u3002";
var LOCID_GRID_TOO_MANY_RECORDS = "\u60a8\u4e00\u6b21\u53ea\u80fd\u9009\u62e9 \u007b0\u007d \u884c\u3002";

function AbsPos(el){
	for (var lx=0,ly=0;el!=null;lx+=el.offsetLeft,ly+=el.offsetTop,el=el.offsetParent);
	return {x:lx,y:ly}
}

function onKeydown(evt, act){
	var iekey = evt.keyCode;
	if(iekey == '13') {
	    eval(act);
	    return false;
	}
}

function document.onkeypress()
{
    var event = window.event;
    if (event.ctrlKey && event.keyCode == 80)
    {
        event.keyCode = 0;
        return false;
    }
}
document.onkeydown = document.onkeypress;

function window.onerror()
{
    return false;
}

function document.oncontextmenu()
{
    //var s = event.srcElement.tagName;
    //event.returnValue = (!event.srcElement.disabled && (document.selection.createRange().text.length > 0 || s == "TEXTAREA" || s == "INPUT" && event.srcElement.type == "text"));
}

function document.onselectstart()
{
    var s = event.srcElement.tagName;
    if (s != "INPUT" && s != "TEXTAREA") event.returnValue = false;
}

var GlobalAllowDrag = false;
function document.ondragstart()
{
    event.returnValue = GlobalAllowDrag;
}

/* encode */
function HtmlAttributeEncode(s)
{
    if (typeof(s) != "string")
    {
        return s;
    }
    s = s.replace(/&/g, "&amp;");
    return s.replace(/\"/g, "&quot;");
}
function HtmlEncode(s)
{
    if (typeof(s) != "string")
    {
        return s;
    }
    s = s.replace(/&/g, "&amp;");
    s = s.replace(/</g, "&lt;");
    s = s.replace(/>/g, "&gt;");
    return s.replace(/\"/g, "&quot;");
}
function XmlEncode(s)
{
    if (IsNull(s))
    {
        return "";
    }
    if (typeof(s) != "string")
    {
        s = s.toString();
    }

    s = escape(s);
    s = s.replace(/%20/g, " ");
    s = s.replace(/%u(\w{4})/g,	"&#x$1;");
    s = s.replace(/%(\w{2})/g,	"&#x$1;");
    return s;
}

/* message */
//修改：添加了IsNull和IsNullOrEmpty方法判断对象的对象的值
//      Jiang 2007-06-18
function alertNull(e, o, t){
    if(t){
        crmTabBar.down(t, true);
    }
    if(IsNullOrEmpty(o.value.trim())){
	    alert('\u8bf7\u8f93\u5165\u3010'+ e +'\u3011\u7684\u5185\u5bb9');
	    //判断对象是否为Null
	    if(!IsNull(o)){
	        o.focus();
	    }
	    return false;
	}
	return true;
}
function alertExist(e, o, t){
    if(t){
        crmTabBar.down(t, true);
    }
	alert('\u6b64\u3010'+ e +'\u3011\u5df2\u7ecf\u5b58\u5728\n\n\u8bf7\u4f7f\u7528\u5176\u5b83\u8f93\u5165');
	if(o){
	    o.focus();
	}
}

//修改：添加了IsNull和IsNullOrEmpty方法判断对象的对象的值
//      添加了 c参数，true为接受函数写定判断方式， false为不按受
//      Jiang 2007-06-18
function alertSelect(e, o, t, c){
    if(t){
        crmTabBar.down(t, true);
    }
    if(!c || IsNullOrEmpty(o.value.trim())){
        alert('\u8bf7\u9009\u62e9\u3010'+ e +'\u3011\u7684\u5185\u5bb9');
        if(!IsNull(o)){
	        o.focus();
	    }
	    return false;
    }
	return true;
}

function alertNumber(o, t){
    if(t){
        crmTabBar.down(t, true);
    }
	alert('\u8bf7\u6b63\u786e\u8f93\u5165\u6570\u5b57');
	if(o){
	    o.focus();
	}
}

/* open dialog */
function OpenDialog(url, width, height){
    //;dialogLeft=" + iLeft + "px;dialogTop=" + iTop + "px
    var params = "dialogWidth:" + width + "px;dialogHeight:" + height + "px;help:0;status:0;scroll:0;center:1;resizable:0;";
    if(url.indexOf('?') > 0)
    {
        url += '&';
    }
    else
    {
        url += '?'
    }

    url += 'token='+ tokenKey();

    return window.showModalDialog(url, window.document, params);
}

/* 右边弹出窗口 梁文刚 2007-6-25 */
function OpenDialogLeft(url, width, height){
    var params = "dialogWidth:" + width + "px;dialogHeight:" + height + "px;dialogLeft:430px;help:0;status:0;scroll:0;center:1;resizable:0;";
    if(url.indexOf('?') > 0)
    {
        url += '&';
    }
    else
    {
        url += '?'
    }

    url += 'token='+ tokenKey();

    return window.showModalDialog(url, window.document, params);
}

function OpenWindow(url, width, height){
	var aw = screen.availWidth;
	var ah = screen.availHeight;

	var left = Math.floor((aw - width) / 2);
	var top = Math.floor((ah - height) / 2);

	var nbrw = window.open(url,'','top='+ top +',left='+ left +',Width='+ (width-8) +', height='+ (height-50) +',menubar=no,scrollbars=auto,status=yes,resizable=no');
	if(!nbrw){
		alert('\u8bf7\u5141\u8bb8\u4f60\u7684IE\u6d4f\u89c8\u5668\u5f39\u51fa\u7a97\u53e3\uff0c\u5426\u5219\u65e0\u6cd5\u767b\u5f55\u3002');
		return;
	}
	nbrw.moveTo(left, top);
	nbrw.focus();
}

var obarid = null;
function OpenNav(barid, url)
{
    var ifr = top.window.frames['LeftFrame'];
    var CS = ifr.document.getElementById('CS');
    ifr.barid = barid;
    ifr.navurl = url;

    if(CS.className == 'area')
    {
        CS.click();
    }
    else
    {
	    if(top.obarid != barid)
	    {
            var href = 'workplace.aspx?barid='+ barid;
            var Workplace = ifr.window.frames['WorkplaceFrame'];
	        Workplace.navigate(href);
	    }
    }

    ifr = top.window.frames['MainFrame'];
    ifr.navigate(url);
}

function Desktop()
{
    var Mainframe = top.window.frames['MainFrame'];
    var LeftFrame = top.window.frames['LeftFrame'];
    var Workplace = LeftFrame.document.getElementById('Workplace');
    Workplace.click();

    var mp_href = 'mainpage.aspx';
    var lo = Mainframe.location.href.toLowerCase();
    if(lo.indexOf(mp_href) == -1)
    {
        Mainframe.navigate(mp_href);
    }
}

/* rewrite url */
function EscapeUrl()
{
	return escape(window.location.pathname + window.location.search);
}
function ReWriteUrl(Url){
	window.location.href = Url +'&url='+ EscapeUrl();
	return false;
}

/* sure to logout */
function Sure2Logout(){
	if(confirm('\u786e\u5b9a\u9700\u8981\u9000\u51fa\u7ba1\u7406\u5e73\u53f0\u5417\uff1f')){
		top.location.href = '../General/Logout.aspx';
	}
}

/* sure to delete */
function Sure2Delete(){
	return confirm('\u786e\u5b9a\u8981\u5220\u9664\u5417\uff1f\n\n\u6ce8\u610f\uff1a\u6570\u636e\u5728\u5220\u9664\u540e\u4e0d\u53ef\u6062\u590d...');
}

/* Orginal CRM JS */
function IsNull(o)
{
	return ("undefined" == typeof(o) || "unknown" == typeof(o) || null == o)
}
function isArray(item)
{
	return item != null && ((item instanceof Array) || (typeof item.splice) == "function");
}
function formatString()
{
	if (arguments.length == 0)
	{
		return "";
	}

	var s = arguments[0];
	var aArguments = arguments;
	if (arguments.length > 1 && isArray(arguments[1])){
		aArguments = arguments[1];

		aArguments.unshift(s);
	}

	for (var i = 1; i < aArguments.length; i++)
	{
		var argIndex = i - 1;
		s = s.replace(new RegExp("\\{" + argIndex + "\\}", "g"), aArguments[i].toString());
	}
	return s;
}
function openPopup()
{
    var oPopup = window.createPopup();
    //SetDefaultStyles(oPopup);
    return oPopup;
}
function SetDefaultStyles(oPopup)
{
    if (!IsNull(oPopup))
    {
        oPopup.document.createStyleSheet("../_common/styles/fonts.css");
    }
}

/* handlers */
function handlerNew()
{
    alert('\u8bf7\u5728\u6240\u5728\u9875\u9762\u5b9e\u73b0\u65b9\u6cd5\uff3bhandlerNew()\uff3d');
}
function handlerDblClick(id)
{
    alert('\u8bf7\u5728\u6240\u5728\u9875\u9762\u5b9e\u73b0\u65b9\u6cd5\uff3bhandlerDblClick()\uff3d');
}
function handlerDelete()
{
    alert('\u8bf7\u5728\u6240\u5728\u9875\u9762\u5b9e\u73b0\u65b9\u6cd5\uff3bhandlerDelete()\uff3d');
}
function handlerClose()
{
    window.opener = null;
    window.close();
}

//添加noShow参数，使其不需要设置点击弹出显示原图窗体
//修改：Jiang 2007-07-18
function ResizeImage(img, width, noShow){
    if(img == null || img.value == '')
        return;
	if(img.width > width){
		img.width = width;
		if(!noShow){
		    img.style.cursor = 'pointer';
		    img.setAttribute("alt", "点击可查看原图片");
		    img.onclick = function(){
			    var mw = screen.availWidth - 10;
			    var mh = screen.availHeight - 30;
			    var imgPath = img.src;
			    var imgIndex = imgPath.lastIndexOf("/");

			    imgPath = imgPath.substring(imgIndex + 1, imgPath.length);
			    //var mw = top.document.body.clientWidth;
			    //var mh = top.document.body.clientHeight;
			    window.open('ShowImage.aspx?src=~/Picture/b/'+ imgPath , 'showimage','left=0,top=0,scrollbars=yes,width='+ mw +',height='+ mh +'');
		    };
		}
	}
}
function GetFileExtImg(f){
	var ex = f.substring(f.lastIndexOf('.')+1);
	return '<img src="../_imgs/file/'+ ex +'.gif" align="absmiddle" border="0" onerror="this.src=\'../_imgs/file/unknow.gif\'" /> ';
}
function ShowAttach(t){
	if(t != ''){
		var html = '';
		var f = '';
		var s = t.split('|');
		for(i=0; i<s.length; i++){
			f = s[i];
			if(f != '')
			{
			    html += '<div style="padding:3px;height:22px;width:170px" class="break">'+ GetFileExtImg(f) +'<a href="files/'+ f +'?dwnld=true' + '">'+ s[i] +'</a></div>'
			}
		}
		document.write(html);
	}
}

/*ken 2007-7-10 检查扩展名*/
function chkext(filename,regex)
{
    if(filename != '')
    {
        var p = '^.+\\.(' + regex + ')$';
        var re = new RegExp(p, 'ig');
        if(re.test(filename) == false)
        {
            var ext = filename.substring(filename.length,filename.lastIndexOf('.'));
            alert('附件不允许上传 ' + ext + '文件.');
            return false;
        }
    }
    return true;
}
/*ken 2007-7-10 检查文件大小*/
function chksize(strFileName,maxsize)
{
    //定义允许文件的大小，单位KB，请根据需要自行修改！
    try{
        var objStream = new ActiveXObject('Scripting.FileSystemObject');
        var filesize=objStream.GetFile(strFileName);
        //alert(filesize.name);
        if(filesize.size == 0 || filesize.size > maxsize)
        {
            alert ('抱歉！您选择的"'+ filesize.name +'"文件为 '+ Math.round(filesize.Size/1024,2) + 'KB,\n不能为 0KB ,或超过了系统 ' + maxsize/1024 + 'KB 的限制！');
            return false;
        }
    }catch(e){}
    return true;
}

/*ken 2007-7-10 删除文件控件路径*/
function Delfilepath(evt, o){

    if(evt.keyCode == 8 || evt.keyCode == 46){
        //alert(o.name);
        o.select();
        return true;
    }
    return false;
}

/*梁文刚 2007-7-25 限制文本框的输入长度 */
/*注：<input type="text" onPropertyChange="return CheckTextLength(this,50);"> */
function CheckTextLength(f,a)
{
    if(f.value.length > parseInt(a))
        f.value = f.value.substring(0,parseInt(a));
}

function openenQuiry(rcm,msg){
window.parent.windowOpen("/res/pages/webframework/common/enquiry.jsp?recallMethod="+ rcm + "&message=" + msg, "failure", name, 185, 380, true, "ifr1", this.window)
}
var temp_show2menu_old=0;
var temp_show2menu_oldId="";
function show2menu(currentId,thisOject,e,parentId)
{
	var dgetID=document.getElementById(currentId);
	var thistop=thisOject.offsetTop;
	var thisleft=thisOject.offsetLeft;

	while(thisOject=thisOject.offsetParent)
	{
		thistop+=thisOject.offsetTop;
		thisleft+=thisOject.offsetLeft;
	}

	var parentDiv = document.getElementById(parentId);
	var left = parseInt(e.clientX);
	var height = parseInt(e.clientY);
	var parentDivX = parseInt(parentDiv.clientWidth, 10);
	var parentDivY = parseInt(parentDiv.clientHeight, 10);
	dgetID.style.display="block";
	var divY = parseInt(dgetID.clientHeight, 10);
	var bodyX = parseInt(document.body.clientWidth, 10);
	var bodyY = parseInt(document.body.clientHeight, 10);
	var newW = 0;
	var newHeight = 0;

    if(left > (bodyX*5/7)){

		newW = thisleft-107;

	}else{

		newW = thisleft+102;
	}



    if(temp_show2menu_oldId==""){
		temp_show2menu_old=thisleft;
		dgetID.style.left=newW;
		temp_show2menu_oldId=currentId;
	}


     if(temp_show2menu_oldId!=currentId || temp_show2menu_old!=thisleft){
		temp_show2menu_oldId=currentId;
		dgetID.style.left=newW;
		temp_show2menu_old=thisleft;

     }


	if(bodyY-height<125){

		dgetID.style.top=thistop-divY+25;
	} else {
		dgetID.style.top=thistop;
	}



}
function hidden2menu(ID)
{
	document.getElementById(ID).style.display="none";
}
function showmy2menu(ID)
{
	document.getElementById(ID).style.display="block";
}
function showmenulist(ID,e)
{
	//alert(e.offsetLeft);
	var thistop=e.offsetTop;
	var thisleft=e.offsetLeft;
	while(e=e.offsetParent)
	{
		thistop+=e.offsetTop;
		thisleft+=e.offsetLeft;
	}
	var dgetID=document.getElementById(ID);
	dgetID.style.left=thisleft+1;//-120;
	dgetID.style.top=thistop+20;
	dgetID.style.display="block";
}
function showmenulist2(ID,e)
{
	//alert(e);
	var thistop=e.offsetTop;
	var thisleft=e.offsetLeft;
	while(e=e.offsetParent)
	{
		thistop+=e.offsetTop;
		thisleft+=e.offsetLeft;
	}
	var dgetID=document.getElementById(ID);
	dgetID.style.left=thisleft+120;
	dgetID.style.top=thistop;
	dgetID.style.display="block";
}

// 2008-5-20 陈彦全
function showmenulist3(id){
	var popdiv = document.getElementById(id);
	popdiv.parentNode.style.position = "relative";
	popdiv.style.poption = "absolute";
    popdiv.style.left = '0px';
	popdiv.style.top = '20px';
	popdiv.style.display='block';
}
// 2008-5-20 陈彦全2
function showmenulist4(id){
	var popdiv = document.getElementById(id);
	popdiv.parentNode.style.position = "relative";
	popdiv.style.poption = "absolute";
    popdiv.style.left = '0px';
	popdiv.style.top = '20px';
	popdiv.style.display='block';
}

function showOrhidden(idName,sh)
{
	var getID=idName.split(",");
	var idNamelist=new Array();
	if(sh=="show")
	{
		for(var i=0;i<getID.length;i++)
		{
			idNamelist[i]=getID[i];
			document.getElementById(idNamelist[i]).style.display="";
		}
	}else if(sh=="hidden")
	{
		for(var j=0;j<getID.length;j++)
		{
			idNamelist[j]=getID[j];
			document.getElementById(idNamelist[j]).style.display="none";
		}
	}
}